﻿
using System;
using System.Collections.Generic;
using Export2ExcelTest;

namespace Export2Excel
{
    public abstract class LevelModelBase
    {
        public object Content { get; private set; }
        public abstract object Id { get; set; }
        public abstract object ParentId { get; set; }
        public abstract int Level { get; set; }

        public LevelModelBase(object content)
        {
            Content = content;
        }

        public static void AnalyseLevelInfo(LevelModelBase above, IEnumerable<LevelModelBase> modelCollection, int maxLevel)
        {
            LevelModelBase currentModel = null;
            Stack<LevelModelBase> levelStack = new Stack<LevelModelBase>();

            foreach (var model in modelCollection)
            {
                SetParentId(ref currentModel, levelStack, model);
            }
        }

        private static void SetParentId(ref LevelModelBase currentModel, Stack<LevelModelBase> levelStack, LevelModelBase model)
        {
            if (currentModel == null)
            {
                currentModel = model;
            }
            else if (model.Level == currentModel.Level)
            {
                model.ParentId = currentModel.ParentId;
                currentModel = model;
            }
            else if (model.Level > currentModel.Level)
            {
                model.ParentId = currentModel.Id;
                levelStack.Push(currentModel);
                currentModel = model;
            }
            else //model.Level < currentModel.Level
            {
                do
                {
                    currentModel = levelStack.Pop();
                } while (model.Level < currentModel.Level);

                SetParentId(ref currentModel, levelStack, model);
            }
        }
    }

    public class FixedAssetLevelPresentation : LevelModelBase
    {
        public const int MaxLevel = 5;

        #region ILevelModel
        public override object Id
        {
            get
            {
                return _content.Id;
            }
            set
            {
                _content.Id = (Guid)value;
            }
        }

        public override object ParentId
        {
            get
            {
                return _content.ParentId;
            }
            set
            {
                _content.ParentId = value as Guid?;
            }
        }

        public override int Level
        {
            get;
            set;
        }
        //public object Id
        //{
        //    get
        //    {
        //        return _content.Id;
        //    }
        //    set
        //    {
        //        _content.Id = (Guid)value;
        //    }
        //}

        //public object ParentId
        //{
        //    get
        //    {
        //        return _content.ParentId;
        //    }
        //    set
        //    {
        //        _content.ParentId = value as Guid?;
        //    }
        //}

        //public int Level { get; set; }
        #endregion

        private string _matchResult;
        private FixedAssetInfo _content;

        private static LevelInfo levelInfo = new LevelInfo();

        public FixedAssetLevelPresentation(FixedAssetInfo model)
            : base(model)
        {
            _content = base.Content as FixedAssetInfo;

            Level = levelInfo.GetLevelInfo(_content.Types, out _matchResult);
            _content.Level = Level;

            if (Level < MaxLevel)
            {
                _content.Name = _matchResult;
            }
        }


    }
}
